package com.ts.content.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.entity.TopicUserEntity;
import com.ts.content.entity.UserEntity;
import com.ts.content.service.TopicUserService;
import com.ts.content.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.*;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TopicUserService topicUserService;

    @GetMapping("/getUserByMobile/{mobile}")
    public UserEntity getUserByMobile(@PathVariable String mobile) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        return userService.getOne(queryWrapper);
    }

    @PostMapping("/addUser")
    public UserEntity addUser(@RequestBody UserEntity userEntity) {
        userService.save(userEntity); // MP会自动回填主键
        return userEntity; // 返回的目的是为了获取主键
    }
	
	
	//修改个人信息
    @PostMapping("userInfoEdit")
    public Result userInfoEdit(@RequestBody UserEntity userEntity, UserLoginDto userLoginDto){
        Integer userId = userLoginDto.getUserId();
        userEntity.setId(userId);
        boolean update = userService.updateById(userEntity);
        UserEntity userEntitybyid = userService.getById(userId);
        return new Result().viewOk("1").ok(userEntitybyid);
    }

    /**
     * 根据圈子id查询用户列表
     * @param page
     * @param id
     * @return
     */
    @GetMapping("/userList")
    public Result userList(@RequestParam Integer page, @RequestParam Integer id){

        Map<String,Object> map1 = new HashMap<>();
        map1.put("topic_id",id);
        List<TopicUserEntity> topicUserEntities = topicUserService.listByMap(map1);
        List<Integer> userIdList = new ArrayList<>();
        for (TopicUserEntity t:topicUserEntities){
            userIdList.add(t.getUserId());
        }
        List<UserEntity> userEntities = userService.listByIds(userIdList);

        return new Result().ok(userEntities);
    }
}